package com.znf4.controller.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.znf4.aop.currentlimit.CurrentLimit;
import com.znf4.constants.CommonConst;
import com.znf4.controller.common.BaseController;
import com.znf4.service.ThirdPartyInterface;



/**
 * 商户登录Controller
 */
@Controller
public class LoginController extends BaseController {
	private static final Log LOG = LogFactory.getLog(LoginController.class);
	

	@Autowired
	private ThirdPartyInterface thirdPartyInterface ;
	
	/**
	 * 函数功能说明 ：登录
	 * 
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@CurrentLimit(limit=5 , timeout=5000)
	@RequestMapping(value = "/login", method ={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String login(HttpServletResponse response ,HttpSession session , Model model) {
		session.removeAttribute(CommonConst.WX_OPEN_ID);
		
		LOG.info("测试 限流 功能");
		
		
		return "system/login";
	}
	
	/**
	 * 函数功能说明 ：退出
	 * 
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	@RequestMapping(value = "/logout", method ={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public String logout(HttpServletRequest request, Model model) {
		
		
		thirdPartyInterface.smsSend();
		
		System.out.println("接口二");
		
		System.out.println("接口三");
		
		return "system/login";
	}
	
	/**
	 * 函数功能说明 ：首页
	 * 
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	/*@RequestMapping(value = "/index", method ={RequestMethod.POST,RequestMethod.GET})
	public String index(HttpServletRequest request, Model model) {
		String codeStatus = SystemConfig.ALIYUN_SMS_STATUS;
		model.addAttribute("codeStatus", codeStatus);
		// 获取登录的用户
        RpUserInfo rpUserInfo = (RpUserInfo)request.getSession().getAttribute(ConstantClass.USER);
        if(rpUserInfo != null){
        	return "system/index";
        }
		
		String mobile = request.getParameter("mobile");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
//		System.out.println("=====>"+mobile+";"+password);
		String msg = "";
		if(StringUtil.isEmpty(mobile)){
			msg = "请输入手机号/密码";
			model.addAttribute("msg", msg);
			return "system/login";
		}
		if(StringUtil.isEmpty(password)){
			msg = "请输入手机号/密码";
			model.addAttribute("msg", msg);
			return "system/login";
		}
		
		rpUserInfo = rpUserInfoService.getDataByMobile(mobile);
		if (rpUserInfo == null) {
			msg = "用户名/密码错误";
		}
		else if(!EncryptUtil.encodeMD5String(password,rpUserInfo.getUserNo()).equals(rpUserInfo.getPassword())){
			msg = "用户名/密码错误";
		}else if(rpUserInfo.getUserType().equals(UserTypeEnum.AGENT.name())|| 
				rpUserInfo.getUserType().equals(UserTypeEnum.NOT_DIRECT_AGENT.name())){
			msg = "该角色无法登录";
		} else{
			if(codeStatus.equals(PublicStatusEnum.ACTIVE.name())){//验证码状态为激活才验证
				if(StringUtil.isEmpty(code)){
					msg = "请输入验证码";
					model.addAttribute("msg", msg);
					return "system/login";
				}
				SmsCodePo codePo = smsCodeService.selectOne(mobile);
				if(codePo == null || !codePo.getCode().equals(code)){
					msg = "验证码错误";
				}
			}
			if(StringUtil.isEmpty(msg)){
				request.getSession().setAttribute(ConstantClass.USER, rpUserInfo);
			}
		}
		
		
		if(!StringUtil.isEmpty(msg)){
			model.addAttribute("mobile", mobile);
			model.addAttribute("password", password);
			model.addAttribute("msg", msg);
			return "system/login";
		}
		return "system/index";
	}*/
	
	/**
	 * 函数功能说明 ：发送短信验证码
	 * 
	 * @参数： @return
	 * @return String
	 * @throws
	 */
	/*@RequestMapping(value = "/sendSms", method ={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ApiCommonResultVo sendSms(HttpServletRequest request, Model model,@RequestParam(value = "mobile") String mobile) throws Exception  {
		RpUserInfo rpUserInfo = rpUserInfoService.getDataByMobile(mobile);
		if (rpUserInfo == null) {
			return new ApiCommonResultVo(-1,"发送失败,用户不存在","");
		}
		String code = RandomUtils.randomInt(4);
		SmsCodePo po = null;
		try {
			po = smsCodeService.insertForAliyun(mobile, code);
		} catch (Exception e) {
			LOG.info("发送短信异常",e);
			return new ApiCommonResultVo(-1,"发送失败,请稍后再试","");
		}
		if(po.getSuccess().equals("true")){
			return new ApiCommonResultVo(0,"发送成功","");
		}else{
			return new ApiCommonResultVo(-1,"发送失败,请稍后再试","");
		}
	}*/

}
